//
//  GYEmoticonBottomView.swift
//  Weibo
//
//  Created by vane on 16/8/18.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

private let tagNumber = 2000
enum GYEmoticonBottomViewType: Int {

    case Default = 0
    
    case Emoji = 1
    
    case Lxh = 2
}
class GYEmoticonBottomView: UIView {
    
    var closure:((type:GYEmoticonBottomViewType)->())?
    
    var currentButton: UIButton?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = RandomColor()
        
        let defaultBtn = addChildButtons("left", title: "默认",type: .Default)
        let emojiBtn = addChildButtons("mid", title: "Emoji",type: .Emoji)
        let lxhBtn = addChildButtons("right", title: "浪小花",type: .Lxh)
        
        defaultBtn.snp_makeConstraints { (make) -> Void in
            
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(emojiBtn)
        }
        
        emojiBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(defaultBtn.snp_trailing)
            make.width.equalTo(lxhBtn)
            
        }
        
        lxhBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emojiBtn.snp_trailing)
            
        }
    }
    
    func addChildButtons(imgName: String, title: String,type:GYEmoticonBottomViewType)->UIButton{
        
        let btn = UIButton()
        
        btn.tag = type.rawValue + tagNumber
        
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_normal"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgName)_selected"), forState: UIControlState.Selected)
        
        btn.setTitle(title, forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        btn.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        if type == .Default {
            btn.selected = true
            currentButton = btn
        }
        
        addSubview(btn)
        
        return btn
    }
    
    
    func setupCurrentButton(tag: Int) {

        let button = viewWithTag(tag + tagNumber) as! UIButton
        
        selectButton(button)
    }

    @objc private func btnClick(button:UIButton){
        
       selectButton(button)
        
        closure?(type: GYEmoticonBottomViewType(rawValue: button.tag - tagNumber)!)
        
    }
    func selectButton(button: UIButton){

        if currentButton == button {
            return
        }
        
        currentButton?.selected = false
        
        button.selected = true

        currentButton = button
    }

}
